﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DaBF.Data;
using DaBF.Models;
using DaBF.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DaBF.Web.Controllers
{
    public class TestController : Controller
    {
        private mySqlDbContext mySqlDb;
        private IService<Category> categoryServer;

        public TestController(mySqlDbContext _DbContext,IService<Category> CategoryServer)
        {
            mySqlDb = _DbContext;
            categoryServer = CategoryServer;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Index([FromForm]Article articles)
        {
            if(articles!=null)
            {
                mySqlDb.Articles.Add(articles);
                
                mySqlDb.SaveChanges();
            }

            return View();
        }

        public IActionResult List()
        {

            return View(mySqlDb.Articles.OrderBy(x => x.id).Skip(2).Take(2));
        }
      
        // GET: /<controller>/
        public IActionResult Data()
        {
            if(mySqlDb.Database.EnsureCreated())
            {
               
            }
            

            return View();
        }

        //---------------------------------------------------------------------------------------------------
        public IActionResult CategoryAdd()
        {
            var t= categoryServer.GetAll();


            List<Category> categorys = Map(mySqlDb.Categorys.OrderBy(x=>x.Order).ToList());
            
            List<SelectListItem> keyValues = GetKsyVaues(categorys,new List<SelectListItem>(),1);
            keyValues.Insert(0, new SelectListItem() { Text = "顶级分类", Value = "0" });
            ViewBag.Categorys = keyValues;

            return View();
        }

        private List<SelectListItem> GetKsyVaues(List<Category> categorys, List<SelectListItem> keyValues,int setp)
        {
            
            foreach (Category item in categorys)
            {
                keyValues.Add(new SelectListItem() { Text =$"{markString(setp, "　　")}{item.CategoryName}", Value = item.id.ToString() });
                GetKsyVaues(item.Subcategories, keyValues,setp+1);
            }
            return keyValues;
        }
        private string markString(int count, string s)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < count; i++)
                sb.Append(s);
            return sb.ToString();
        }
        List<Category> getSub(Category c)
        {
           return  c.Subcategories;
        }

        [HttpPost]
        public IActionResult CategoryAdd(Category category)
        {
            mySqlDb.Categorys.Add(category);
            mySqlDb.SaveChanges();
            return RedirectToAction("CategoryList");
        }

        public IActionResult CategoryList()
        {
            return View(mySqlDb.Categorys);
        }


        public List<Category> Map(List<Category> list)
        {
            foreach (Category item in list)
            {
                if(item.ParentCategoryId!=0)
                {
                    list.Single(x => x.id == item.ParentCategoryId).Subcategories.Add(item);
                }
            }
            return list.Where(x=>x.ParentCategoryId==0).ToList();
        }
       
    }
}
